/**
 * 回款配票 - 编辑页面
 * @author miaoyl
 * @date 2019/01/28
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        layui = win.layui,
        vueData = {
            isAdd: false,
            isCheck: false,
            amount: 3,
            form:{
                GWEPMLEGAL_NAME: null,
                GWEPPLANT_NAME: null,
                GWEP003_NAME: null,
                GWEP005_NAME: null,
                GWEP004_NAME: null
            },
            rules: {
                /*GWEPMLEGAL: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEPPLANT: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEPDOCDATE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEPDOCNO: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEP003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEP005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWEP004: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ]*/
            }
        },
        cols1 = [[
            {field: 'GWEPSEQ',type: 'checkbox', title: '选择'},
            {field: 'GWEP001', width: 100, title: '回款单号',event: "openGwep001"}
            , {field: 'GWEPDOCDATE', width: 80, title: '单据日期'}
            , {field: 'GWEP002', width: 100, title: '回款单项次'}
            , {field: 'GWEO001', width: 100, title: '款别', event: "open_gwdc018"}
            , {field: 'GWEO002', width: 80, title: '币别'}
            , {field: 'GWEO003', width: 80, title: '汇率'}
            , {field: 'GWEP008', width: 100, title: '原币金额', edit: 'text'}
            , {field: 'GWEP009', width: 100, title: '本币金额', edit: 'text'}
            , {field: 'GWEPDOCNO', width: 100, title: '核销单号'}
            , {field: 'GWEPMLEGAL_NAME', width: 100, title: '归属法人'}
            , {field: 'GWEPPLANT_NAME', width: 100, title: 'ERP组织'}
            , {field: 'GWEP003_NAME', width: 80, title: '客户'}
            , {field: 'GWEP005_NAME', width: 80, title: '归属部门', edit: 'text'}
            , {field: 'GWEP004_NAME', width: 140, title: '业务人员', edit: 'text'}
            , {field: 'GWEP010', width: 140, title: '备注', edit: 'text'}
            , {field: 'MAX_GWEP008', width: 100, title: '最大原币金额', hide: true }
            , {field: 'MAX_gwep009', width: 100, title: '最大本币金额', hide: true }
        ]],
        cols2 = [[
            {field: 'GWEQ101',type: 'checkbox', title: '选择'}
            , {field: 'GWEQDOCDATE', width: 80, title: '单据日期'}
            , {field: 'GWEQ001', width: 80, title: '来源',
                filterDatas: app.datas_('crm_gwei022_gweq001'),
                edit:'select'}
            , {field: 'GWEQ002', width: 140, title: '发票单号', event: 'openGweq002'}
            , {field: 'GWEQ003', width: 160, title: '发票单项次'}
            , {field: 'GWEQ004', width: 100, title: '出货单号'}
            , {field: 'GWEQ005', width: 80, title: '出货单项次'}
            , {field: 'GWEQ006', width: 80, title: '订单单号'}
            , {field: 'GWEQ007', width: 80, title: '订单项次'}
            , {field: 'GWEQ008_NAME', width: 80, title: '客户'}
            , {field: 'GWEQ009_NAME', width: 80, title: '业务人员'}
            , {field: 'GWEQ010_NAME', width: 80, title: '业务部门'}
            , {field: 'GWEQ011', width: 80, title: '币别'}
            , {field: 'GWEQ012', width: 80, title: '汇率', edit: 'text'}
            , {field: 'GWEQ013', width: 80, title: '原币金额', edit: 'text'}
            , {field: 'GWEQ014', width: 80, title: '本币金额', edit: 'text'}
           /* , {field: 'GWEQ015', width: 80, title: '本币税额', edit: 'text'}*/
            , {field: 'GWEQ016', width: 80, title: '备注'}
            , {field: 'GWEA006', width: 80, title: '税率', hide: true}
            , {field: 'MAX_GWEQ013', width: 80, title: '原币金额最大值', hide: true}
            , {field: 'MAX_GWEQ014', width: 80, title: '本币金额最大值', hide: true}
        ]],
        callObj,
        table_where = {},
        table_select_data = [],
        table,
        gwei022Table1,
        gwei022Table2,
        layOpenIndex,
        moduleName
    ;

    var gwei022Edit = app.Module.extend({
            init: function () {
                self = this;
                self._super({
                    _tag: 'gwei022_edit',
                    _loadTplsFull: [
                        {
                            dom: 'common_open_window', tpl: 'common/_open_window'
                        },
                        {
                            dom: 'common_open_org', tpl: 'common/_open_org'
                        },
                        {
                            dom: 'common_open_ywy', tpl: 'common/_open_ywy'
                        }/*,
                        {
                            dom: 'common_open_gweo', tpl: 'common/_open_gweo'
                        },
                        {
                            dom: 'common_open_gwef', tpl: 'common/_open_gwef'
                        }*/
                    ]
                }, win);
            },

            initTableHead: function () {//回款单
                layui.use(['table'], function () {
                    table = layui.table;
                    gwei022Table1 = table.render({
                        elem: app.getByid(doc, 'gwei022_head_table'),
                        id: 'gwei022_head_table',
                        cols: cols1,
                        height: '250',
                        page: false
                    });

                    table.on('tool(gwei022_head_table)', function (obj) {
                        var tableData = gwei022Table1.getDatas();
                        if (obj.event === 'openGwep001') {//回款单号开窗
                            var choose_data = [];
                            var count = 0;
                            for(var i=0;i<tableData.length;i++){
                                var str = tableData[i].GWEP001 + tableData[i].GWEP002;
                                choose_data.push(str);
                            }
                            if(tableData.length > 0){
                                count = tableData[tableData.length-1].GWEPSEQ;
                            }
                            var dataList = [];
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    multiple: true,
                                    config: {
                                        columnList: [{label: '回款单编号', prop: 'GWEODOCNO', width: '150'},
                                            {label: '项次', prop: 'GWEOSEQ', width: '150'},
                                            {label: '单据日期', prop: 'GWEODOCDATE', width: '150'},
                                            {label: '款别', prop: 'GWEO001', width: '150'},
                                            {label: '币种', prop: 'GWEO002', width: '150'},
                                            {label: '汇率', prop: 'GWEO003', width: '150'},
                                            {label: '原币收款金', prop: 'GWEO004', width: '150'},
                                            {label: '本币收款金', prop: 'GWEO005', width: '150'},
                                            {label: '原币已冲金额', prop: 'GWEP008_SUM', width: '150'},
                                            {label: '本币已冲金额', prop: 'GWEP009_SUM', width: '150'}
                                            ],
                                        url: 'crm/gwei022/getGweoData',
                                        dataList: dataList,
                                        key: 'ROW_ID',
                                        searchParam:{
                                            STR: choose_data.join(','),
                                            GWEP003 : vueData.form.GWEP003,
                                            GWEPPLANT : vueData.form.GWEPPLANT,
                                            GWEPDOCNO : vueData.form.GWEPDOCNO
                                        }
                                    },
                                    cb: function (data) {
                                        for(var i=0;i<data.length;i++){
                                            if(i == 0){
                                                obj.update({
                                                    'GWEP001': data[i].GWEODOCNO,
                                                    'GWEP002': data[i].GWEOSEQ,
                                                    'GWEO001': data[i].GWEO001,
                                                    'GWEO002': data[i].GWEO002,
                                                    'GWEO003': data[i].GWEO003,
                                                    'GWEP008': data[i].GWEO004 - data[i].GWEP008_SUM,
                                                    'GWEP009': data[i].GWEO005 - data[i].GWEP009_SUM,
                                                    'MAX_GWEP008': data[i].GWEO004 - data[i].GWEP008_SUM,
                                                    'MAX_GWEP009': data[i].GWEO005 - data[i].GWEP009_SUM
                                                });
                                            }else {
                                                tableData.push({
                                                    'GWEPSEQ': count + i + 1,
                                                    'GWEP001': data[i].GWEODOCNO,
                                                    'GWEPDOCDATE': vueData.form.GWEPDOCDATE,
                                                    'GWEP002': data[i].GWEOSEQ,
                                                    'GWEO001': data[i].GWEO001,
                                                    'GWEO002': data[i].GWEO002,
                                                    'GWEO003': data[i].GWEO003,
                                                    'GWEP008': data[i].GWEO004 - data[i].GWEP008_SUM,
                                                    'GWEP009': data[i].GWEO005 - data[i].GWEP009_SUM,
                                                    'GWEPDOCNO': vueData.form.GWEPDOCNO,
                                                    'GWEPMLEGAL_NAME': vueData.form.GWEPMLEGAL_NAME,
                                                    'GWEPPLANT_NAME' : vueData.form.GWEPPLANT_NAME,
                                                    'GWEP003_NAME': vueData.form.GWEP003_NAME,
                                                    'GWEP005_NAME': vueData.form.GWEP005_NAME,
                                                    'GWEP004_NAME': vueData.form.GWEP004_NAME,
                                                    'GWEP010': '',
                                                    'MAX_GWEP008': data[i].GWEO004 - data[i].GWEP008_SUM,
                                                    'MAX_GWEP009': data[i].GWEO005 - data[i].GWEP009_SUM
                                                });
                                            }

                                        }
                                        self.tableHeadReload(tableData);
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择回款单信息',
                                    area: ['1000px', '430px']
                                }
                            });
                        }
                    });

                    //监听行单击事件
                    table.on('row(gwei022_head_table)', function (obj) {
                        table_select_data = obj.data;
                        obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
                    });

                    table.on('edit(gwei022_head_table)', function (obj) {
                        var table_data = gwei022Table1.getDatas();
                        if (obj.field == 'GWEP008') {//原币金额
                            var GWEP008 = obj.value;
                            if(parseFloat(GWEP008) < 0 || parseFloat(GWEP008) > obj.data.MAX_GWEP008){
                                obj.updateOfEdit({
                                    'GWEP008': obj.data.MAX_GWEP008
                                });
                                app.showWarning("本次冲销的原币金额应大于0且不大于"+ obj.data.MAX_GWEP008);
                                return ;
                            }
                            var GWEP009 = math.div(GWEP008, table_data.GWEO003).toFixed(3);
                            if(parseFloat(GWEP009) > obj.data.MAX_GWEP009){
                                obj.updateOfEdit({
                                    'GWEP009': obj.data.MAX_GWEP009
                                });
                            }else {
                                obj.updateOfEdit({
                                    'GWEP009': GWEP009
                                });
                            }
                        } else if (obj.field == 'GWEP009') {
                            var GWEP009 = obj.value;
                            if(parseFloat(GWEP009) < 0 || parseFloat(GWEP009) > obj.data.MAX_GWEP009){
                                obj.updateOfEdit({
                                    'GWEP009': obj.data.MAX_GWEP009
                                });
                                app.showWarning("本次冲销的本币金额应大于0且不大于"+ obj.data.MAX_GWEP009);
                                return ;
                            }
                        }
                    });

                });
            },

            initTableDetail: function () {//发票信息
                layui.use(['table'], function () {
                    table = layui.table;
                    gwei022Table2 = table.render({
                        elem: app.getByid(doc, 'gwei022_detail_table'),
                        id: 'gwei022_detail_table',
                        cols: cols2,
                        height: '250',
                        page: false
                    });

                    table.on('tool(gwei022_detail_table)', function (obj) {
                        var table_data = gwei022Table2.getDatas();
                        if (obj.event === 'openGweq002') { //开票
                            if(obj.data.GWEQ001 == '1') {
                                var choose_data = [];
                                var count = 0;
                                for (var i = 0; i < table_data.length; i++) {
                                    var str = table_data[i].GWEQ002 + table_data[i].GWEQ003;
                                    choose_data.push(str);
                                }
                                if (table_data.length > 0) {
                                    count = table_data[table_data.length - 1].GWEQ101;
                                }
                                self._openTplFull({
                                    tag: 'common_open_window',
                                    tagData: {
                                        multiple: true,
                                        config: {
                                            columnList: [{label: '发票单号', prop: 'GWEFDOCNO', width: '150'},
                                                {label: '项次', prop: 'GWEFSEQ', width: '150'},
                                                {label: '单据日期', prop: 'GWEFDOCDATE', width: '150'},
                                                {label: '出货单号', prop: 'GWEF003', width: '150'},
                                                {label: '出货项次', prop: 'GWEF101', width: '150'},
                                                {label: '订单号', prop: 'GWEF004', width: '150'},
                                                {label: '订单项次', prop: 'GWEC101', width: '150'},
                                                {label: '币种', prop: 'GWEF006', width: '150'},
                                                {label: '汇率', prop: 'GWEF007', width: '150'},
                                                {label: '原币金额', prop: 'GWEF011', width: '150'},
                                                {label: '本币金额', prop: 'GWEF015', width: '150'},
                                                {label: '本币税额', prop: 'GWEF016', width: '150'},
                                                {label: '原币已冲金额', prop: 'GWEQ013_SUM', width: '150'},
                                                {label: '本币已冲金额', prop: 'GWEQ014_SUM', width: '150'},
                                                {label: '税率', prop: 'GWEA006', width: '150'}
                                            ],
                                            url: 'crm/gwei022/getGwefData',
                                            dataList: [],
                                            key: 'ROW_ID',
                                            searchParam:{
                                                GWEQ013: vueData.form.GWEPDOCNO,
                                                STR: choose_data.join(','),
                                                GWEP003 : vueData.form.GWEP003,
                                                GWEPPLANT : vueData.form.GWEPPLANT
                                            }
                                        },
                                        cb: function (data) {
                                            for (var i = 0; i < data.length; i++) {
                                                if (i == 0) {
                                                    obj.update({
                                                        'GWEQ002': data[i].GWEFDOCNO,
                                                        'GWEQ003': data[i].GWEFSEQ,
                                                        'GWEQ004': data[i].GWEF003,
                                                        'GWEQ005': data[i].GWEF101,
                                                        'GWEQ006': data[i].GWEF004,
                                                        'GWEQ007': data[i].GWEC101,
                                                        'GWEQ011': data[i].GWEF006,
                                                        'GWEQ012': data[i].GWEF007,
                                                        'GWEQ013': data[i].GWEF011 - data[i].GWEQ013_SUM,
                                                        'GWEQ014': data[i].GWEF015 - data[i].GWEQ014_SUM,
                                                        'GWEQ015': self.calculate(math.div(math.mul(math.sub(data[i].GWEF015, data[i].GWEQ014_SUM), data[i].GWEA006), 100),vueData.amount),
                                                        'GWEA006': data[i].GWEA006,
                                                        'MAX_GWEQ013': data[i].GWEF011 - data[i].GWEQ013_SUM,
                                                        'MAX_GWEQ014': data[i].GWEF015 - data[i].GWEQ014_SUM
                                                    });
                                                } else {
                                                    table_data.push({
                                                        'GWEQ101': count + i + 1,
                                                        'GWEQDOCDATE': vueData.form.GWEPDOCDATE,
                                                        'GWEQ001': '1',
                                                        'GWEQ002': data[i].GWEFDOCNO,
                                                        'GWEQ003': data[i].GWEFSEQ,
                                                        'GWEQ004': data[i].GWEF003,
                                                        'GWEQ005': data[i].GWEF101,
                                                        'GWEQ006': data[i].GWEF004,
                                                        'GWEQ007': data[i].GWEC101,
                                                        'GWEQ008_NAME': vueData.form.GWEP003_NAME,
                                                        'GWEQ009_NAME': vueData.form.GWEP004_NAME,
                                                        'GWEQ010_NAME': vueData.form.GWEP005_NAME,
                                                        'GWEQ011': data[i].GWEF006,
                                                        'GWEQ012': data[i].GWEF007,
                                                        'GWEQ013': data[i].GWEF011 - data[i].GWEQ013_SUM,
                                                        'GWEQ014': data[i].GWEF015 - data[i].GWEQ014_SUM,
                                                        'GWEQ015': self.calculate(math.div(math.mul(math.sub(data[i].GWEF015, data[i].GWEQ014_SUM), data[i].GWEA006), 100),vueData.amount),
                                                        'GWEP016': '',
                                                        'GWEA006': '',
                                                        'MAX_GWEP008': data[i].GWEO004 - data[i].GWEP008_SUM,
                                                        'MAX_GWEP009': data[i].GWEO005 - data[i].GWEP009_SUM
                                                    });
                                                }
                                            }
                                            self.tableDetaillReload(table_data);
                                        }
                                    },
                                    index: 0,
                                    openOpts: {
                                        title: '选择发票信息',
                                        area: ['600px', '550px']
                                    }
                                });
                            }
                        }
                    });

                    table.on('edit(gwei022_detail_table)', function (obj) {
                        var tableData = gwei022Table2.getDatas();
                        if (obj.field == 'GWEQ001') {
                            var GWEQ001 = obj.value;
                            var GWEQ011 = '';
                            var GWEA006 = '';
                            if(GWEQ001 == '2'){
                                app.ajax({//获取税率等信息
                                    xurl: 'crm/gwei022/getGwak',
                                    xdata: {GWEP003: vueData.form.GWEP003},
                                    async: false,
                                    cb: function (result) {
                                        if (result.ok) {
                                            GWEQ011 = result.data.data.GWAK008;
                                            GWEA006 = result.data.data.GWEA006;
                                        }
                                    }
                                });
                                obj.updateOfEdit({
                                    GWEQ011: GWEQ011,
                                    GWEA006: GWEA006,
                                    MAX_GWEQ013: 0,
                                    MAX_GWEQ014: 0
                                });
                            }
                        }else if(obj.field == 'GWEQ012'){//汇率
                            var GWEQ012 = obj.value;
                            var GWEQ013 = tableData.GWEQ013;
                            var GWEQ014 = self.calculate(math.div(GWEQ013, GWEQ012), vueData.amount);
                            if(obj.data.GWEQ001 == '1' && parseFloat(GWEQ014) > obj.data.MAX_GWEQ014){
                                GWEQ014 = obj.data.MAX_GWEQ014;
                            }
                            obj.updateOfEdit({
                                GWEQ014: GWEQ014,
                                GWEQ015: self.calculate(math.div(math.mul(GWEQ014, obj.data.GWEA006), 100), vueData.amount)
                            });
                        }else if(obj.field == 'GWEQ013'){// 原币
                            var GWEQ013 = obj.value;
                            if(obj.data.GWEQ001 == '1' && parseFloat(GWEQ013) > obj.data.MAX_GWEQ013){
                                app.showWarning("本次冲销的原币金额应大于0且不大于"+ obj.data.MAX_GWEQ013);
                                obj.updateOfEdit({
                                    GWEQ013: obj.data.MAX_GWEQ013
                                });
                                GWEQ013 = obj.data.MAX_GWEQ013;
                                return ;
                            }
                            var GWEQ014 = (GWEQ013 / obj.data.GWEQ012).toFixed(3);
                            if(obj.data.GWEQ001 == '1' && parseFloat(GWEQ014) > obj.data.MAX_GWEQ014){
                                GWEQ014 = obj.data.MAX_GWEQ014;
                            }
                            obj.updateOfEdit({
                                GWEQ014: GWEQ014,
                                GWEQ015: (GWEQ014 * obj.data.GWEA006 / 100).toFixed(3)
                            });
                        }else if(obj.field == 'GWEQ014'){// 本币
                            var GWEQ014 = obj.value;
                            if(obj.data.GWEQ001 == '1' && parseFloat(GWEQ014) > obj.data.MAX_GWEQ014){
                                app.showWarning("本次冲销的本币金额应大于0且不大于"+ obj.data.MAX_GWEQ014);
                                obj.updateOfEdit({
                                    GWEQ014: obj.data.MAX_GWEQ014
                                });
                                GWEQ014 = obj.data.MAX_GWEQ014;
                                return ;
                            }
                            obj.updateOfEdit({
                                GWEQ014: GWEQ014,
                                GWEQ015: self.calculate(math.div(math.mul(GWEQ014, obj.data.GWEA006), 100), vueData.amount)
                            });
                        }
                    });

                });
            },

            tableHeadReload: function (listData1) {
                self._reloadTable({id: 'gwei022_head_table', data: listData1});
            },

            tableDetaillReload: function (listData2) {
                self._reloadTable({id: 'gwei022_detail_table', data: listData2 });
            },

            addAllData: function (listData1, listData2) {
                app.ajax({
                    xurl: 'crm/gwei022/addData',
                    xdata: {list1: listData1, list2: listData2, GWEP: [vueData.form]},
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName + '增加成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwei022').reloadTableCurr();
                            //self._closeTpl();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            editAllData: function (listData1, listData2) {
                app.ajax({
                    xurl: 'crm/gwei022/editData',
                    xdata: {list1: listData1, list2: listData2, GWEP: [vueData.form]},
                    loading: true,
                    toJsonStr: true,
                    okmsg: moduleName + '修改成功',
                    cb: function (result) {
                        if (result.ok) {
                            app.pageModule('gwei022').reloadTableCurr();
                            //self._closeTpl();
                            layer.close(layOpenIndex);
                        }
                    }
                });
            },

            _call: function (tagData) {
                moduleName = tagData.moduleName;
                layOpenIndex = tagData.layOpenIndex;
                if(tagData.is_check){
                    vueData.isCheck = true;
                }else {
                    vueData.isCheck = false;
                }
                if (tagData.is_add) {
                    vueData.isAdd = true;
                    app.ajax({
                        xurl: 'crm/gwei022/getAddData',
                        cb: function (result) {
                            if (result.ok) {
                                app.extendUpLow({
                                    target: vueData.form,
                                    res: result.data.data,
                                    reset: true
                                });
                                var listData1 = [];
                                var listData2 = [];
                                self.tableHeadReload(listData1);
                                self.tableDetaillReload(listData2);
                            }
                        }
                    });
                } else {
                    vueData.isAdd = false;
                    callObj = tagData.obj;
                    app.extendUpLow({
                        target: vueData.form,
                        res: callObj.data,
                        reset: true
                    });
                    app.ajax({
                        xurl: 'crm/gwei022/getGwepData',
                        xdata: {GWEPDOCNO: callObj.data.GWEPDOCNO},
                        loading: true,
                        cb: function (result) {
                            if(result.ok){
                               var listData1 = result.data.data;
                                self.tableHeadReload(listData1);
                            }
                        }
                    });
                    app.ajax({
                        xurl: 'crm/gwei022/getGweqData',
                        xdata: {GWEQ103: callObj.data.GWEPDOCNO},
                        loading: true,
                        cb: function (result) {
                            if(result.ok){
                                var listData2 = result.data.data;
                                self.tableDetaillReload(listData2);
                            }
                        }
                    });
                }
                app.ajax({
                    xurl: 'admin/getDigit',
                    cb: function (result) {
                        if(result.ok) {
                            var data = result.data.data;
                            vueData.amount = Math.pow(10,data.amount);
                        }
                    }
                });
            },

            calculate:function (num,digit) {
                var data = math.div(Math.round(math.mul(num, digit)),digit);
                return data;
            },

            _ready: function () {
                table = layui.table;
                vueData.form = app.fields_(['GWEP_T'],vueData.form);
                var vue = new Vue({
                    el: '#gwei022_edit',
                    data: vueData,
                    watch: {},
                    methods: {
                        cancle: function () {
                            self._closeTpl();
                        },
                        addHead: function () {
                            if (vueData.form.GWEP003_NAME == '' || vueData.form.GWEP003_NAME == null
                                || vueData.form.GWEPMLEGAL_NAME == '' || vueData.form.GWEPMLEGAL_NAME == null) {
                                app.showWarning("请先选择客户及归属法人！");
                                return;
                            }
                            var listData1 = gwei022Table1.getDatas();
                            var index = listData1.length;
                            var num = 0;
                            if (index != 0) {
                                num = listData1[index - 1].GWEPSEQ;
                            }
                            listData1.push({
                                GWEPSEQ: num + 1,
                                GWEP001: '',
                                GWEPDOCDATE: vueData.form.GWEPDOCDATE,
                                GWEP002: '',
                                GWEO002: '',
                                GWEO003: '',
                                GWEP008: '',
                                GWEP009: '',
                                GWEPDOCNO: vueData.form.GWEPDOCNO,
                                GWEPMLEGAL_NAME: vueData.form.GWEPMLEGAL_NAME,
                                GWEPPLANT_NAME: vueData.form.GWEPPLANT_NAME,
                                GWEP003_NAME: vueData.form.GWEP003_NAME,
                                GWEP005_NAME: vueData.form.GWEP005_NAME,
                                GWEP004_NAME: vueData.form.GWEP004_NAME,
                                GWEP010: '',
                                MAX_GWEP008: '',
                                MAX_GWEP009: ''
                            });
                            self.tableHeadReload(listData1);
                        },
                        addDetail: function () {
                            if (vueData.form.GWEP003_NAME == '' || vueData.form.GWEP003_NAME == null
                                || vueData.form.GWEPMLEGAL_NAME == '' || vueData.form.GWEPMLEGAL_NAME == null) {
                                app.showWarning("请先选择客户及归属法人！");
                                return;
                            }
                            var listDataHead = gwei022Table1.getDatas();
                            if(listDataHead.length == 0 && listDataHead[0].GWEP001 == ''){
                                app.showWarning("请先选择回款单！");
                                return;
                            }
                            var listData2 = gwei022Table2.getDatas();
                            var count = 0;
                            if (listData2.length > 0) {
                                count = listData2[listData2.length -1].GWEQ101;
                            }
                            listData2.push({
                                'GWEQ101': count + 1,
                                'GWEQDOCDATE': vueData.form.GWEPDOCDATE,
                                'GWEQ001': '1',
                                'GWEQ002': '',
                                'GWEQ003': '',
                                'GWEQ004': '',
                                'GWEQ005': '',
                                'GWEQ006': '',
                                'GWEQ007': '',
                                'GWEQ008_NAME': vueData.form.GWEP003_NAME,
                                'GWEQ009_NAME': vueData.form.GWEP004_NAME,
                                "GWEQ010_NAME": vueData.form.GWEP005_NAME,
                                'GWEQ011': '',
                                'GWEQ012': '',
                                'GWEQ013': '',
                                'GWEQ014': '',
                                'GWEQ015': '',
                                'GWEQ016': '',
                                'GWEA006': '',
                                'MAX_GWEQ013': '',
                                'MAX_GWEQ014': ''
                            });
                            self.tableDetaillReload(listData2);
                        },
                        delHead: function () {
                            var list = gwei022Table1.getDatas();
                            var checkStatus = table.checkStatus('gwei022_head_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWEPSEQ: v.GWEPSEQ
                                    });
                                });
                                var listData1 = list;
                                self.tableHeadReload(listData1);
                            }
                        },
                        delDetail: function () {
                            var list = gwei022Table2.getDatas();
                            var checkStatus = table.checkStatus('gwei022_detail_table');
                            if (checkStatus.data.length > 0) {
                                checkStatus.data.forEach(function (v) {
                                    list.removeByMap({
                                        GWEQ101: v.GWEQ101,
                                    });
                                });
                                var listData2 = list;
                                self.tableDetaillReload(listData2);
                            }
                        },
                        saveData: function () {
                            this.$refs['form'].validate(function (valid) {
                                if (valid) {
                                    var listData_1 = gwei022Table1.getDatas();
                                    var listData_2 = gwei022Table2.getDatas();
                                    if (listData_1.length == 0 || listData_2.length == 0) {
                                        app.showError("表格信息不可为空！");
                                        return;
                                    }
                                    var gwep008_sum = 0;
                                    var gwep009_sum = 0;
                                    var gweq013_sum = 0;
                                    var gweq014_sum = 0;
                                    for (var j = 0; j <= listData_1.length - 1; j++){
                                        gwep008_sum = math.add(gwep008_sum , parseFloat(listData_1[j].GWEP008));
                                        gwep009_sum = math.add(gwep009_sum , parseFloat(listData_1[j].GWEP009));
                                    }
                                    for (var j = 0; j <= listData_2.length - 1; j++) {
                                        gweq013_sum = math.add(gweq013_sum , parseFloat(listData_2[j].GWEQ013));
                                        gweq014_sum = math.add(gweq014_sum , parseFloat(listData_2[j].GWEQ014));
                                    }
                                    if (gwep008_sum != gweq013_sum) {
                                        app.showWarning("回款与核销原币总金额不一致，请核对后保存！");  //"回款与核销原币总金额不一致，请核对后保存！"
                                            return;
                                    }
                                    if (gwep009_sum != gweq014_sum) {
                                        app.showWarning("回款与核销本币总金额不一致，请核对后保存！");  //"回款与核销本币总金额不一致，请核对后保存！"
                                        return;
                                    }
                                    listData_1.addMap(vueData.form, false);
                                    /*listData_1.push.apply(listData_1,listData_2);*/
                                    if (vueData.isAdd) {
                                        self.addAllData(listData_1, listData_2);
                                    } else {
                                        self.editAllData(listData_1, listData_2);
                                    }
                                }
                            });
                        },
                        openGwepmlegal: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'cq_sysorg_biz_1',
                                        columnList: [
                                            {label: '法人编号', prop: 'ID', width: '150'},
                                            {label: '法人名称', prop: 'NAME', width: '150'},
                                            {label: 'ERP组织编号', prop: 'AZP01', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWEPMLEGAL_NAME = data.NAME;
                                        vueData.form.GWEPMLEGAL = data.ID;
                                        // vueData.form.GWEPPLANT_NAME = data.BIZ_CODE_NAME;
                                        vueData.form.GWEPPLANT = data.AZP01;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择归属法人',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openGwep003: function () {
                            var listData1 = gwei022Table1.getDatas();
                            if (vueData.form.GWEP003 != '' && vueData.form.GWEP003 != null) {
                                if(listData1.length > 0){
                                    app.confirm({
                                        msg: '修改客户将清空表格信息，是否确认修改？',
                                        ok: function () {
                                            listData1 = [];
                                            var listData2 = [];
                                            self.tableHeadReload(listData1);
                                            self.tableDetaillReload(listData2);
                                            self._openTplFull({
                                                tag: 'common_open_window',
                                                tagData: {
                                                    config: {
                                                        method: 'cq_gwak001_1',
                                                        columnList: [
                                                            {label: '客户编号', prop: 'GWAK001', width: '150'},
                                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                                            {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                                        ],
                                                        // url: 'crm/gwei022/getGwakData'
                                                    },
                                                    cb: function (data) {
                                                        vueData.form.GWEP003 = data.GWAK001;
                                                        vueData.form.GWEP003_NAME = data.GWAKL003;
                                                    }
                                                },
                                                index: 0,
                                                openOpts: {
                                                    title: '选择客户信息',
                                                    area: ['600px', '470px']
                                                }
                                            });
                                        }
                                    });
                                }
                            }
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'cq_gwak001_1',
                                        columnList: [
                                            {label: '客户编号', prop: 'GWAK001', width: '150'},
                                            {label: '客户简称', prop: 'GWAKL003', width: '150'},
                                            {label: '客户全称', prop: 'GWAKL004', width: '150'}
                                        ],
                                        // url: 'crm/gwei022/getGwakData'
                                    },
                                    cb: function (data) {
                                        vueData.form.GWEP003 = data.GWAK001;
                                        vueData.form.GWEP003_NAME = data.GWAKL003;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择客户信息',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openGwep005: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysorg_id',
                                        columnList: [
                                            {label: '部门编号', prop: 'ID', width: '150'},
                                            {label: '部门名称', prop: 'NAME', width: '150'}
                                        ],
                                        // url: 'crm/gwei022/getGwakData'
                                    },
                                    cb: function (data) {
                                        vueData.form.GWEP005_NAME = data.NAME;
                                        vueData.form.GWEP005 = data.ID;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择归属部门',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        openGwep004: function () {
                            self._openTplFull({
                                tag: 'common_open_window',
                                tagData: {
                                    config: {
                                        method: 'q_sysuser_id',
                                        columnList: [
                                            {label: '业务员编号', prop: 'ID', width: '150'},
                                            {label: '业务员名称', prop: 'NAME', width: '150'}
                                        ]
                                    },
                                    cb: function (data) {
                                        vueData.form.GWEP004_NAME = data.NAME;
                                        vueData.form.GWEP004 = data.ID;
                                    }
                                },
                                index: 0,
                                openOpts: {
                                    title: '选择业务员',
                                    area: ['600px', '470px']
                                }
                            });
                        },
                        clearMlegal:function () {
                            if(vueData.form.GWEPMLEGAL_NAME != '' || vueData.form.GWEPMLEGAL_NAME != null){
                                vueData.form.GWEPPLANT_NAME = '';
                                vueData.form.GWEPPLANT = '';
                            }
                        }
                    },
                    created: function () {
                        self._setEleIsReady('gwei022_edit');
                    }
                });

                self.initTableHead();
                self.initTableDetail();
            }
            ,
            _lazy: function () {
                this._super();
            }
        })
    ;

    return new gwei022Edit();

})
;